package handler

import (
	"github.com/dgrijalva/jwt-go"
	"github.com/gin-gonic/gin"
	jwt2 "github.com/golangblogs/gojwt"
	"goods/api/request"
	"goods/consts"
	"goods/internal/dao"
	"goods/internal/model"
	"time"
)

func TokenEvaluates(c *gin.Context) {
	token, err := jwt2.NewJWT(consts.JWT_SECRET_KEY).CreateToken(jwt2.CustomClaims{
		ID: c.GetUint("userId"),
		StandardClaims: jwt.StandardClaims{
			ExpiresAt: time.Now().Unix() + 86400,
		},
	})
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "token获取失败",
			"data": err.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": token,
	})
}

func CreateEvaluates(c *gin.Context) {
	var req request.CreateEvaluatesReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	var good model.Goods
	dao.GetOneByFields(&model.Goods{Id: req.GoodsId}, &good)
	if good.Id != req.GoodsId {
		c.JSON(200, gin.H{
			"code": 10001,
			"msg":  "商品不存在",
			"data": nil,
		})
		return
	}
	var user model.Users
	dao.GetOneByFields(&model.Users{Id: req.UsersId}, &user)
	if user.Id != req.UsersId {
		c.JSON(200, gin.H{
			"code": 10002,
			"msg":  "用户未登录",
			"data": nil,
		})
		return
	}
	evaluates := model.Evaluates{
		UsersId: req.UsersId,
		GoodsId: req.GoodsId,
		Comment: req.Comment,
	}
	if !dao.Create(&evaluates) {
		c.JSON(200, gin.H{
			"code": 10003,
			"msg":  "评论失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "评论成功",
		"data": evaluates,
	})
}

func QueryEvaluates(c *gin.Context) {
	var req request.QueryEvaluatesReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	var good model.Goods
	dao.GetOneByFields(&model.Goods{Id: req.GoodsId}, &good)
	if good.Id != req.GoodsId {
		c.JSON(200, gin.H{
			"code": 10001,
			"msg":  "商品不存在",
			"data": nil,
		})
		return
	}
	var eva []model.Evaluates
	dao.GetListByFields(&model.Evaluates{GoodsId: req.GoodsId}, &eva)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": eva,
	})
}

func UpdateEvaluates(c *gin.Context) {
	var req request.UpdateEvaluatesReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	var user model.Users
	dao.GetOneByFields(&model.Users{Id: req.UsersId}, &user)
	if user.Id != req.UsersId {
		c.JSON(200, gin.H{
			"code": 10001,
			"msg":  "用户未登录",
			"data": nil,
		})
		return
	}
	var good model.Goods
	dao.GetOneByFields(&model.Goods{Id: req.GoodsId}, &good)
	if good.Id != req.GoodsId {
		c.JSON(200, gin.H{
			"code": 10002,
			"msg":  "商品不存在",
			"data": nil,
		})
		return
	}
	var eva model.Evaluates
	dao.GetOneByFields(&model.Evaluates{Id: req.Id}, &eva)
	if eva.Id != req.Id {
		c.JSON(200, gin.H{
			"code": 10003,
			"msg":  "评论不存在",
			"data": nil,
		})
		return
	}
	evaluates := model.Evaluates{
		Id:      req.Id,
		UsersId: req.UsersId,
		GoodsId: req.GoodsId,
		Comment: req.Comment,
	}
	if !dao.Update(&evaluates) {
		c.JSON(200, gin.H{
			"code": 10004,
			"msg":  "修改失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "修改成功",
		"data": evaluates,
	})
}

func DeleteEvaluates(c *gin.Context) {
	var req request.DeleteEvaluatesReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	var user model.Users
	dao.GetOneByFields(&model.Users{Id: req.UsersId}, &user)
	if user.Id != req.UsersId {
		c.JSON(200, gin.H{
			"code": 10001,
			"msg":  "用户未登录",
			"data": nil,
		})
		return
	}
	var good model.Goods
	dao.GetOneByFields(&model.Goods{Id: req.GoodsId}, &good)
	if good.Id != req.GoodsId {
		c.JSON(200, gin.H{
			"code": 10002,
			"msg":  "商品不存在",
			"data": nil,
		})
		return
	}
	var eva model.Evaluates
	dao.GetOneByFields(&model.Evaluates{Id: req.Id}, &eva)
	if eva.Id != req.Id {
		c.JSON(200, gin.H{
			"code": 10003,
			"msg":  "评论不存在",
			"data": nil,
		})
		return
	}
	evaluates := model.Evaluates{
		Id:      req.Id,
		UsersId: req.UsersId,
		GoodsId: req.GoodsId,
	}
	if !dao.Delete(&evaluates) {
		c.JSON(200, gin.H{
			"code": 10004,
			"msg":  "删除失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "删除成功",
		"data": nil,
	})
}
